package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.DeviceCategoryMapper;
import com.dmpp.mes.dao.MaterialClassificationMapper;
import com.dmpp.mes.parameterInfo.DeviceCategory;
import com.dmpp.mes.parameterInfo.MaterialClassification;
import com.dmpp.mes.service.DeviceCategoryService;
import com.dmpp.mes.service.MaterialClassificationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "MaterialClassificationService")

public class MaterialClassificationServiceImpl implements MaterialClassificationService {

    @Autowired
    private MaterialClassificationMapper mapper;

    @Override
    public ResponseData findAll(MaterialClassification materialClassification) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(materialClassification));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(String code) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(code));
        return responseData;
    }

    @Override
    public ResponseData insert(MaterialClassification materialClassification) {
        ResponseData responseData = new ResponseData();
        MaterialClassification materialClassification1 = mapper.findByCode(materialClassification.getCode());
        if (materialClassification1 == null) {
            mapper.insert(materialClassification);
        } else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(MaterialClassification materialClassification) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(materialClassification);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(String code) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(code);
        return responseData;
    }

    @Override
    public ResponseData delete(String code) {
        ResponseData responseData = new ResponseData();
        //查看原材料材质分类
        MaterialClassification categoryNumber = mapper.categoryNumber(code);
        //查看原材料类型
        MaterialClassification typeNumber = mapper.typeNumber(code);
        //查看原材料材质
        MaterialClassification materialQualityNumber = mapper.materialQualityNumber(code);
        if (categoryNumber.getNumber() == 0) {
            if (typeNumber.getNumber() == 0) {
                if (materialQualityNumber.getNumber() == 0) {
                    mapper.delete(code);
                } else {
                    responseData.setCodeEnum(RespEnum.ERROR1015);
                }
            } else {
                responseData.setCodeEnum(RespEnum.ERROR1014);
            }
        } else {
            responseData.setCodeEnum(RespEnum.ERROR1013);
        }
        return responseData;
    }

}
